package common;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class FilterIndex {
    private static List<String> resultTitleList = new ArrayList<>();

    /**
     * 递归获取json二叉树的title键值
     * @param treeArray  调用getDataIndexValueTreeList接口返回responseData的 json树
     * @param prefix
     */
    public static List getTitle(JSONArray treeArray, String prefix) {
        if (treeArray != null && treeArray.size() > 0) {
            for (Object object : treeArray) {
                JSONObject jsonObject = (JSONObject) object;
                JSONArray children = jsonObject.getJSONArray("children");

                /**
                 * 参数的格式自己定义结构
                 * */
                String nextPrefix = (Objects.equals(prefix, "") ? "" : prefix + ",") + jsonObject.get("title");

                getTitle(children, nextPrefix);
            }
        }else{
            /**
             * ---------------------------------------------------
             * prefix 就是你要的东西，放到一个全局变量里面就可以到处使用了
             * ---------------------------------------------------
             * */
            resultTitleList.add(prefix);

        }

        return resultTitleList;
    }
}
